﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Shop.Core.Share;
using Shop.IServices.Admin;
using Shop.Model.Dto.Admin.JurUserRole;
using Shop.Model.View.Admin.JurUserRole;

namespace Shop.AdminApi.Controllers;

/// <summary>
/// 角色管理
/// </summary>
[ApiController]
[Route("admin/[controller]")]
public class JurRoleController: ShopControllerBase
{
    private readonly IJurRoleService _roleService;

        public JurRoleController(IJurRoleService roleService)
        { 
            _roleService = roleService;
        }

        /// <summary>
        /// 添加角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Add")]
        [Authorize("Permission")]
        public async Task<AppResult> Add(UserRoleAddDto dto)
        {
            try
            {
                return await _roleService.Add(dto);
            }
            catch (Exception ex)
            {
                return Problem(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Modify")]
        [Authorize("Permission")]
        public async Task<AppResult> Modify(UserRoleModifyDto dto)
        {
            try
            {
                return await _roleService.Modify(dto);
            }
            catch (Exception ex)
            {
                return Problem(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 删除角色
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("Delete")]
        [Authorize("Permission")]
        public async Task<AppResult> Delete(DeleteDto dto)
        {
            try
            {
                return await _roleService.Delete(dto);
            }
            catch (Exception ex)
            {
                return Problem(ex.Message.ToString());
            }
        }

        /// <summary>
        /// 获取角色列表
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpGet("Page")]
        [Authorize("Permission")]
        public  AppResult<PageModel<SystemUserRoleVo>> GetRoleList([FromQuery] SysRolePageSearchDto dto)
            => _roleService.PageSearchRole(dto);

        /// <summary>
        /// 获取所有对象
        /// </summary>
        /// <returns></returns>
        [HttpGet("List")]
        public async Task<AppResult<List<SystemUserRoleVo>>> GetAllRoleList()
            => await _roleService.GetAllRoleList();

        /// <summary>
        /// 添加权限
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost("SetSysMenu")]
        [Authorize("Permission")]
        public async Task<AppResult> AddAccess(UserRoleAddMenuDto dto)
             => await _roleService.AddAccess(dto);

        /// <summary>
        /// 角色权限树
        /// </summary>
        /// <param name="roleId"></param>
        /// <returns></returns>
        [HttpGet("GetSysMenu")]
        public async Task<AppResult<SysMenuTreeVo[]>> GetRoleMenuTree([FromQuery]long roleId)
            => await _roleService.GetRoleMenuTree(roleId);
}